﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using PanasonicWarranty.Business;
using System.Web.Script.Serialization;
using System.Collections;
using Settings;

namespace PanasonicWarranty.UI
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]

    public class WebService : System.Web.Services.WebService
    {
        #region MO-MT
        [WebMethod]
        public object GetMT(Guid mo_guid)
        {
            return SmsMtStatistic.GetByMoGuid(mo_guid)
                        .Select(p => new
                        {
                            p.MtGuid,
                            p.MoGuid,
                            p.ShortCode,
                            p.DestAddr,
                            p.OperatorID,
                            p.Message,
                            p.CooperateID,
                            p.Status,
                            p.SentTime,
                            p.CDRIndicator,
                            p.MTType
                        });
        }

        [WebMethod]
        public object GetMO(Guid mo_guid)
        {
            return SmsMoStatistic.GetByMoGuid(mo_guid)
                        .Select(p => new
                        {
                            p.MoGuid,
                            p.ShortCode,
                            PhoneNumber = p.Subs,
                            p.OperatorID,
                            p.Message,
                            p.CooperateID,
                            p.Status,
                            p.ReceivedTime
                        });
        }

        [WebMethod]
        public object GetMO_MT(Guid mo_guid)
        {
            var mt = SmsMtStatistic.GetByMoGuid(mo_guid)
                        .Select(p => new
                        {
                            p.MtGuid,
                            p.MoGuid,
                            p.ShortCode,
                            PhoneNumber = p.DestAddr,
                            p.OperatorID,
                            p.Message,
                            p.CooperateID,
                            p.Status,
                            p.SentTime,
                            p.CDRIndicator,
                            p.MTType
                        });
            var mo = SmsMoStatistic.GetByMoGuid(mo_guid)
                       .Select(p => new
                       {
                           p.MoGuid,
                           p.ShortCode,
                           PhoneNumber = p.Subs,
                           p.OperatorID,
                           p.Message,
                           p.CooperateID,
                           p.Status,
                           p.ReceivedTime
                       });
            return new ArrayList { mo, mt };
        }
        #endregion

        #region Authorize
        [WebMethod]
        public bool Authorize()
        {
            try
            {
                SysUser currUser = SysUser.GetByID(int.Parse(HttpContext.Current.User.Identity.Name));
                if (currUser != null)
                {
                    if (currUser.UserName.Equals(Constants.AdminRootAccount, StringComparison.CurrentCultureIgnoreCase))
                        return true;
                    return SysRole.CheckRole(currUser.UserID, HttpContext.Current.Request.UrlReferrer.AbsolutePath, Role.Write);
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod]
        public bool Authorizes(string roles)
        {
            try
            {
                string[] roleParam = roles.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < roleParam.Length; i++)
                    roleParam[i] = roleParam[i].Trim();
                SysUser currUser = SysUser.GetByID(int.Parse(HttpContext.Current.User.Identity.Name));
                if (currUser != null)
                {
                    if (currUser.UserName.Equals(Constants.AdminRootAccount, StringComparison.CurrentCultureIgnoreCase))
                        return true;
                    return SysRole.CheckRole(currUser.UserID, HttpContext.Current.Request.UrlReferrer.AbsolutePath, roleParam);
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion Authorize
    }
}
